package com.leenleda.ips.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.leenleda.ips.R;
import com.leenleda.ips.databinding.ItemCrashBinding;
import com.leenleda.ips.util.LogUtils;

import java.util.List;

public class CrashListAdapter extends RecyclerView.Adapter<CrashListAdapter.BindViewHolder> {

    private Context context;
    public List<String> dataList;
    public int choicePosition = 0;

    public CrashListAdapter(Context context, List<String> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }
    @Override
    public BindViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ItemCrashBinding binding = ItemCrashBinding.bind(LayoutInflater.from(context).inflate(R.layout.item_crash,parent,false));
        return new BindViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(BindViewHolder holder, final int position) {
        final ItemCrashBinding binding=(ItemCrashBinding) holder.getBinding();

        binding.itemText.setText(dataList.get(position));
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("SDVNSD", "-----getRoot------" + position);
                listener.onClickItem(dataList.get(position));
            }
        });
        binding.itemText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickItem(dataList.get(position));
                LogUtils.e("SDVNSD", "-----itemText------" + position);
            }
        });
    }

    class BindViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;
        public BindViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
        public ViewDataBinding getBinding() {
            return binding;
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        //        void onClickItem(String updateUrl,String name, int position);
        void onClickItem(String name);
    }

    public void setOnItemClick(OnItemClickListener listener){
        this.listener = listener;
    }
}